package com.qfedu.service.impl;

import com.qfedu.mapper.ItemMapper;
import com.qfedu.model.Item;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.service.IItemService;
import com.qfedu.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/3 0:03
 */

@Service
public class ItemServiceImpl implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();

        List<Item> page = itemMapper.findPage((currentPage - 1) * pageSize, pageSize);

        Long count = itemMapper.count();

        return new PageResult(count,page);

    }

    @Override
    @Transactional
    public int addItem(Item item) {
        item.setIid(UUIDUtils.uuid());
        return itemMapper.addItem(item);
    }

    @Override
    public Item findByIid(String iid) {
        return itemMapper.findByIid(iid);
    }

    @Override
    @Transactional
    public int edit(Item item) {
        item.setUpdatetime(new Date());
        int row = itemMapper.updateItem(item);
        return row;
    }

    @Override
    @Transactional
    public int deleteByIid(String iid) {
        int row = itemMapper.deleteByIid(iid);
        return row;
    }
}
